package com.seean.easy.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seean.easy.constant.CommonConstant;
import com.seean.easy.constant.R;
import com.seean.easy.entity.UserEntity;
import com.seean.easy.request.UserRequest;
import com.seean.easy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author seean
 * @date 2020/09/14 14:00
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户", tags = "UserController")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户信息分页
     *
     * @param request
     * @return
     */
    @GetMapping("/getUserPage")
    @ApiOperation("用户信息分页")
    public R getUserPage(Page page, UserRequest request) {

        //----------------query way
        //page
        userService.page(page, Wrappers.<UserEntity>query().lambda().like(UserEntity::getUserName, request.getUserName()));

        //list
        userService.list(Wrappers.<UserEntity>query().lambda().like(UserEntity::getUserName, request.getUserName()));

        // asc/desc
        userService.list(Wrappers.<UserEntity>query().lambda()
                .like(UserEntity::getUserName, request.getUserName())
                .orderByAsc(UserEntity::getAge));

        // in
        String[] names = {"tom", "lucy"};
        userService.list(Wrappers.<UserEntity>query().lambda().in(UserEntity::getUserName, names).orderByAsc(UserEntity::getAge));

        //  or ， >-gt ， <-lt ，  >= - ge  ，<= - gt
        userService.list(Wrappers.<UserEntity>query().lambda().and(StringUtils.isNotEmpty(request.getUserName()),
                f -> f.like(UserEntity::getUserName, request.getUserName())
                        .or()
                        .like(UserEntity::getSign, request.getUserName()))
                .ge(null != request.getBirth(), UserEntity::getBirth, request.getBirth()));

        //------------------------------stream------

        // foreach,filter,sorted
        List<UserEntity> users = userService.list();
        users.stream().filter(v -> v.getAge().intValue() > 10)
                .sorted(Comparator.comparing(UserEntity::getAge))
                .forEach(v -> System.out.println(v.getUserName()));

        //通过 某一列 转 list
        List<String> nameList = users.stream().map(UserEntity::getUserName).collect(Collectors.toList());

        //通过 某一列 转 map  进行分组
        Map<Integer, List<UserEntity>> ageMap = users.stream().collect(Collectors.groupingBy(UserEntity::getAge));

        //自定义sql
        List<UserEntity> list=userService.getUserBySex("1");

        return new R<>(userService.page(page, Wrappers.<UserEntity>query().lambda().like(UserEntity::getUserName, request.getUserName())));
    }

    /**
     * 用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation("新增用户信息")
    public R addUser(@RequestBody UserEntity user) {
        userService.save(user);
        return new R<>(CommonConstant.SUCCESS);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation("修改用户信息")
    public R updateUser(@RequestBody UserEntity user) {
        userService.updateById(user);
        return new R<>(CommonConstant.SUCCESS);
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation("删除用户信息")
    @Transactional(rollbackFor = Exception.class)
    public R deleteUser(@PathVariable("id") String id) {
        userService.removeById(id);
        return new R<>(CommonConstant.SUCCESS);
    }

}

    